package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.activity.WebViewActivity;
import com.youmingxing.adpter.selfInfo.PopularityAdapter;
import com.youmingxing.bean.Web;
import com.youmingxing.bean.selfinfo.Popularity;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.view.DialogManager;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * @author evilyin(ChenZhixi)
 * @since 2015-9-1
 * 我的人气页面
 */

public class PopularityActivity extends BaseActivity implements View.OnClickListener, RefreshListView.OnLoadMoreListener {


    private RefreshListView listView;
    private View headerView;

    private PopularityAdapter adapter;
    private List<Popularity> popularityList;

    private TextView totalValueTv;
    private int mark = 1;

    public PopularityActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_list_view);
        setTopbarName(R.string.popularity_title);
        popularityList = new ArrayList<>();
        listView = (RefreshListView) findViewById(R.id.list_view);
        DialogManager.showDialog(this);
        SelfInfoApi.getMyPopularity(mark, popHandler);
    }

    @Override
    public void onErrorClick() {
        SelfInfoApi.getMyPopularity(mark, popHandler);
    }

    private void initView() {
        listView.setOnLoadMoreListener(this);
        headerView = getLayoutInflater().inflate(R.layout.activity_popularity_top, null);
        TextView getPopTv = (TextView) headerView.findViewById(R.id.get_pop_method);
        getPopTv.setOnClickListener(this);
        totalValueTv = (TextView) headerView.findViewById(R.id.popularity_values);
        listView.addHeaderView(headerView);
        adapter = new PopularityAdapter(this);
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_pop_method:
                Intent intent = new Intent(PopularityActivity.this, WebViewActivity.class);
                intent.putExtra("web_page", Web.popularity);
                startActivity(intent);
                break;
        }
    }

    BaseJsonHandler popHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            showErrorPage(false);
            if (headerView == null){
                initView();
            }
            JSONArray data = response.optJSONObject("data").optJSONArray("item");
            int total = response.optJSONObject("data").optInt("total");
            totalValueTv.setText(total+"");
            if (mark == 1 && data.length()==0){
                listView.noData("您当前还没有人气值哦");
            }
            mark++;
            if (data.length()<20){
                listView.noMoreData();
            }
            Popularity bean;
            for (int i = 0; i < data.length(); i++) {
                bean = new Popularity(data.optJSONObject(i));
                popularityList.add(bean);
            }
            adapter.setData(popularityList);
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            listView.endLoadMore();
            showErrorPage(true);
        }
    };


    @Override
    public void onLoadMore() {
        SelfInfoApi.getMyPopularity(mark, popHandler);
    }
}
